# -*- coding: utf-8 -*-

# @File  : supplier_star.py
# @Author: Lomo
# @Site  : lomo.space
# @Date  : 2019-12-03
# @Desc  : 大数据计算


from model.supplier_star import SupplierStar
from utils.utill import Utils


class SupplierStarCal(object):
    def __init__(self):
        self.supplier_star = SupplierStar()

    def calculate(self, index_name, supplier_id, date):
        """
        按指标进行十分制换算(正向)
        :param index_name: 指标名
        :param supplier_id: 供应商 id
        :param date: 日期
        :return:
        """
        current_value = self.supplier_star.get_by_index_id_date(index_name, supplier_id, date)[0]
        max_value = self.supplier_star.get_max_index_by_date(index_name, date)[0]
        min_value = self.supplier_star.get_min_index_by_date(index_name, date)[0]
        if max_value - min_value == 0:
            return 0
        else:
            return (current_value - min_value) / (max_value - min_value) * 10

    def _calculate(self, index_name, supplier_id, date):
        """
        按指标进行十分制换算(逆向), 5项: tms_collect/tms_timeout/tms_sham/css_back/css_supplier_back
        :param index_name: 指标名
        :param supplier_id: 供应商 id
        :param date: 日期
        :return:
        """
        current_value = self.supplier_star.get_by_index_id_date(index_name, supplier_id, date)[0]
        max_value = self.supplier_star.get_max_index_by_date(index_name, date)[0]
        min_value = self.supplier_star.get_min_index_by_date(index_name, date)[0]
        if max_value - min_value == 0:
            return 0
        else:
            return (max_value - current_value) / (max_value - min_value) * 10

    def tms_24(self, supplier_id, date):
        """
        24H揽件率
        :param supplier_id:
        :param date:
        :return:
        """
        index_name = Utils.get_function_name()
        return self.calculate(index_name, supplier_id, date)

    # 逆向指标: 揽收到签收时间＞5天、超时发货、虚假发货、整体退款退货率、供应商原因退款退货率.

    def tms_collect(self, supplier_id, date):
        """
        揽收到签收时间＞5天
        :param supplier_id:
        :param date:
        :return:
        """
        index_name = Utils.get_function_name()
        return self._calculate(index_name, supplier_id, date)

    def tms_timeout(self, supplier_id, date):
        """
        超时返货
        :param supplier_id:
        :param date:
        :return:
        """
        index_name = Utils.get_function_name()
        print(index_name)
        return self._calculate(index_name, supplier_id, date)


